package com.vcredit.creditcat.manager.wallet;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IMyWalletManager;
import com.vcredit.creditcat.dto.wallet.WalletAmtInOutLogDto;
import com.vcredit.creditcat.dto.wallet.WalletDrawHandlerResultDto;
import com.vcredit.creditcat.dto.wallet.WalletDrawInputDto;
import com.vcredit.creditcat.dto.wallet.WalletDrawLogDto;
import com.vcredit.creditcat.dto.wallet.WalletIndexDto;
import com.vcredit.creditcat.model.WalletAmt;
import com.vcredit.creditcat.service.wallet.MyWalletService;
import com.vcredit.creditcat.service.wallet.WalletAmtLogService;
import com.vcredit.creditcat.service.wallet.WalletAmtService;

@Service(protocol = { "protocol1" }, version="1.0")
public class MyWalletManager implements IMyWalletManager{
	
	@Autowired
	private MyWalletService  myWalletService;
	
	@Autowired
	private WalletAmtService  walletAmtService;
	
	@Autowired
	private WalletAmtLogService  walletAmtLogService;

	/**
	 * 我的钱包首页
	 */
	@Override
	public WalletIndexDto index(Long custId) throws Exception {
		return myWalletService.index(custId);
	}

	/**
	 * 钱包余额提现
	 */
	@Override
	public void drawAmt(WalletDrawInputDto param, Long custId) throws Exception {
		myWalletService.drawAmt(param, custId);
	}

	@Override
	public WalletAmtInOutLogDto walletAmtHisTory(Long custId) {
		return myWalletService.walletAmtHisTory(custId);
	}

	@Override
	public WalletDrawHandlerResultDto handlerUploadFile(
			Map<String, String> param) {
		
		return myWalletService.handlerUploadFile(param);
	}

	@Override
	public List<WalletDrawLogDto> queryWalletLog(WalletDrawLogDto param) {
		return myWalletService.queryWalletLog(param);
	}

	@Override
	public boolean updateWalletDrawStatus(WalletDrawLogDto param) {
		return myWalletService.updateWalletDrawStatus(param);
	}
	
	
	@Override
	public BigDecimal getAccumulatedAmt(Long custId){
		BigDecimal accumulatedAmt = walletAmtLogService.getAccumulatedAmt(custId);		
		if(accumulatedAmt==null){
			accumulatedAmt = new BigDecimal(0);
		}
		BigDecimal balance =  this.getBalanceAmt(custId);
		accumulatedAmt = accumulatedAmt.add(balance);
		return accumulatedAmt;		
	}
	
	@Override
	public BigDecimal getBalanceAmt(Long custId){
		BigDecimal balance = new BigDecimal(0) ;
		List<WalletAmt> list = walletAmtService.find(new WalletAmt(custId, null, null, null));
		if(list.size()>0){
			balance = new BigDecimal(list.get(0).getAmt());
		}		
		return balance;		
	}
}
